﻿<%@ Page Language="C#" AutoEventWireup="false" CodeFile="sUserform.aspx.cs" Inherits="KPIReport.Web.Forms.UserManagement.sUserform" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>User Form</title>
    <script src="<%= ResolveUrl("~/scripts/jquery.inputmask.bundle.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.sha1.js") %>" type="text/javascript"></script>
    <link href="<%= ResolveUrl("~/styles/chosen/chosen.min.css") %>" rel="stylesheet"
        type="text/css" />
    <script src="<%= ResolveUrl("~/scripts/chosen/chosen.jquery.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.msgBox.v1.js") %>" type="text/javascript"></script>
    <script type="text/javascript">

        $("#btnSave").button({ icons: { primary: "ui-icon-disk"} });
        $("#btnClose").button({ icons: { primary: "ui-icon-close"} });
 
    </script>


    <script type="text/javascript">

        $(document).ready(function () {
               $('#txtUserCode').focus();
            // 1. prepare the validation rules and messages.
            var rules = {
                txtUserName: {
                    required: true,
                    maxlength: 100
                },

                txtUserCode: {
                    required: true,
                    email : true,
                    maxlength: 50
                },

                txtMobile: {
                    maxlength: 15
                },

//                txtEmail: {
//                    required: true,
//                    email: true,
//                    maxlength: 50
//                },

                txtPassword: {
                    required: true,
                    maxlength: 50
                },
                txtConfirmPassword: {
                    required: true,
                    maxlength: 50
                }

            };
            var messages = {
                txtUserName: {
                    required: "Nama user wajib di isi!",
                    maxlength: "Panjang maksimal nama user adalah 100 digit"
                },

                txtUserCode: {
//                    required: "Kode user wajib di isi!",
                //                    maxlength: "Panjang maksimal kode user adalah 20 digit"
                    required: "Email wajib di isi",
                    email: "Format alamat email harus berformat xxx@domain.xxx",
                    maxlength: "Panjang maksimal email adalah 50 digit"
                },

                txtMobile: {
                    maxlength: "Panjang maksimal nomor handphone adalah 15 digit"
                },


            };

            //-------------- validasi -----------------
            //txtUsername tidak boleh angka dan karakter lainnya
            $("#txtUserName").inputmask("Regex", { regex: "[A-Za-z-.,0-9 ]*" });

//            //txtUserCode tidak boleh karakter khusus
//            $("#txtUserCode").inputmask("Regex", { regex: "[A-Za-z0-9._-]*" });

            //txtMobile tidak boleh huruf dan karakter khusus
            $("#txtMobile").inputmask("Regex", { regex: "[0-9]*" });
            var id = $('#<%= hfID.ClientID %>').val();
         

            // 2. Initiate the validator
            var validator = new jQueryValidatorWrapper("form1", rules, messages);

            // 3. Set the click event to do the validation
            $("#btnSave").click(function () {

                if (!validator.validate())
                    return;


                var status = $('#<%= hfStatus.ClientID %>').val();
                var objUser = new Object();
                objUser.UserID = id;
                objUser.UserName = encodeURIComponent($('#txtUserName').val());
                objUser.UserCode = encodeURIComponent($('#txtUserCode').val());
//                objUser.Email = encodeURIComponent($('#txtEmail').val());
//                objUser.Mobile = encodeURIComponent($('#txtMobile').val());

                var save = true;
//                if (typeof ($("#ddlPosition option:selected").val()) != 'undefined') {
//                    objUser.PositionID = encodeURIComponent(ddlPosition);
//                    if (objUser.PositionID.toString() == "" && status == -1) {

////                        alert("Jabatan harus di pilih!");
////                        save = false;
//                    }
//                }
//                else {
//                    objUser.PositionID = '';
//                }

                //objUser.CabangID = $("#ddlCabang option:selected").val();
                objUser.Password = encodeURIComponent($.sha1("welcome"));
                var IsActive = "0";
                var checkIsActive = document.getElementById("chkIsActive");
                if (checkIsActive.checked) {
                    IsActive = "1";
                }
                else {
                    IsActive = "0";
                }
                objUser.IsActive = encodeURIComponent(IsActive);
                jsonData = JSON.stringify(objUser);

                if (save == true) {
                    if (id == 0) {

                        jQuery.ajax({

                            type: 'POST',
                            url: '<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sUserhandler.ashx?method=cekusercode&code=' + objUser.UserCode + '") %>',
                            contentType: 'application/json',
                            dataType: 'json',
                            success: function (data) {
                                if (data.Sector == "True") {
                                    alert("Data dengan kode user " + objUser.UserCode + " sudah ada! ");
                                    $('#UserCode').val('');
                                    $('#UserCode').focus();

                                }
                                else {
                                    CallAjax('<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sUserhandler.ashx") %>', '?method=insert', jsonData);
                                    parent.$('#dialog').dialog('close');

                                    setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);

                                    return false;
                                }
                            },
                            error: function () {
                                alert('Error!');
                            }
                        });


                    }
                    else {

                        CallAjax('<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sUserhandler.ashx") %>', '?method=update', jsonData);
                        parent.$('#dialog').dialog('close');

                        setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);

                        return false;
                    }

                }


            });


        });

    </script>
   
    <script type="text/javascript">

        function ClearText() {

            $('#txtUserName').val('');
            $('#txtUserCode').val('');
        }

        $('#btnClose').click(function () {
            parent.$('#dialog').dialog('close');
        });
         
    </script>
</head>
<body onload="ClearText();">
    <form id="form1" runat="server">
    <table border="0">
        <tr>
            <td style="width: 50">
                Email
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtUserCode" runat="server" Width="300"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Nama User
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtUserName" MaxLength="100" runat="server" Width="300"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
            </td>
        </tr>
        
     
        <tr id="trPassword" runat="server" visible="false">
            <td style="width: 50">
                Password
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtPassword" Text="welcome" MaxLength="10" runat="server" TextMode="Password"
                    Width="300"></asp:TextBox>&nbsp;<span style="color: Red;">*</span>
            </td>
        </tr>
        <tr id="trConfirmPassword" runat="server" visible="false">
            <td style="width: 50">
                Konfirmasi Password
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtConfirmPassword" Text="welcome" MaxLength="10" runat="server"
                    TextMode="Password" Width="300"></asp:TextBox>&nbsp;<span style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Aktif
            </td>
            <td>
                :
            </td>
            <td>
                <asp:CheckBox ID="chkIsActive" runat="server" />
            </td>
        </tr>
   <%--     <tr>
            <td style="width: 50">
                Cabang
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlCabang" runat="server" Width="300"></asp:DropDownList>
            </td>
        </tr>--%>
      <%--  <tr>
            <td style="width: 50">
                Departemen
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlDepartment" runat="server" Width="400">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Jabatan
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlPosition" runat="server" Width="400">
                </asp:DropDownList>
            </td>
        </tr>--%>
        <tr>
            <td colspan="3">
                <div id="msgErrorChangePassword" style="color: Red; font: 11px Arial,Helvetica,sans-serif;
                    height: 19px; width: 300px;">
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <div style="text-align: right; padding-top: 10px;">
                    <button id="btnSave" type="button">
                        Save</button>
                    <button id="btnClose" type="button">
                        Close</button>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <asp:HiddenField ID="hfID" runat="server" />
                <asp:HiddenField ID="hfStatus" runat="server" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
